package com.just521.xinshoutong.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.just521.fastjson.R.color;
import com.just521.xinshoutong.Activity_note_edit_text;
import com.just521.xinshoutong.R;

public class NoteTextView  extends LinearLayout
implements NoteElePlugin{
	public static final int FLAG_TO_EDIT_TEXT =0x100;
	
	String mcontentInit;
	String mcontent;
	Activity msrc;
	
	TextView mText;
	public NoteTextView(Context context) {
		super(context);
		init(context,null);
	}
	
	public NoteTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context, attrs);
	}
	private void init(Context context, AttributeSet attrs){
		setBackgroundResource(R.drawable.text_field_bg);
		setPadding(10, 10, 10, 10);
		setOrientation(LinearLayout.HORIZONTAL);
	}
	public void orignalData(Object data){
		if(data!=null){
			
			mcontentInit=(String)data;
		}
	}
	public void initData(Object data){
		if(data==null){
			insertItem(null);
		}else{
			// insert items view
			mcontent=(String)data;
			insertItem(mcontent);
		}
		
		//insert edit view
		
	}
	public void setActivity(Activity src){
		msrc=src;
	}
	public Object getData(){
		return mcontent;
	}
	private void insertItem(String text){
		if(mText==null){
			
			mText =new TextView(getContext());
			
			mText.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			
			addView(mText);
		}
		if(text!=null){
			mText.setTextColor(color.black_gray);
			mText.setText(text);
		}
	}
	
	public void showEditView(){
		toTextEditActivity(mcontent);
	}
	public void onLongClick(){
		
	}
	public void hideSoftInput(){
	}
	public String getViewType(){
		return "text";
	}
	public boolean isDataChanged(){
		boolean b=false;
		if(mcontent!=null && !mcontent.equals(mcontentInit)){
			b=true;
		}
		return b;
	}
	private void toTextEditActivity(String idc){
		Intent intent = new Intent(msrc, Activity_note_edit_text.class);
		if(idc!=null){
//			intent.putExtra("sourceid", idc.getId());
			intent.putExtra("sourcetext", idc);
		}
		msrc.startActivityForResult(intent,FLAG_TO_EDIT_TEXT);
	}
}
